package com.wj.console.graphql.service;

import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLSchema;
import static graphql.schema.GraphQLObjectType.newObject;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.wj.console.graphql.entity.User;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.Scalars.GraphQLInt;
import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLArgument.newArgument;

@Service
public class GraphSchema {

	private GraphQLSchema schema;

	private GraphQLOutputType userType;

	public GraphSchema() {
		initOutputType();
		schema = GraphQLSchema.newSchema()
				.query(newObject().name("GraphQuery").field(createUsersField()).field(createUserField()).build())
				.build();
	}

	private void initOutputType() {
		/**
		 * 会员对象结构
		 */
		userType = newObject().name("User").field(newFieldDefinition().name("id").type(GraphQLInt).build())
				.field(newFieldDefinition().name("age").type(GraphQLInt).build())
				.field(newFieldDefinition().name("sex").type(GraphQLInt).build())
				.field(newFieldDefinition().name("name").type(GraphQLString).build())
				.field(newFieldDefinition().name("pic").type(GraphQLString).build()).build();
	}

	/**
	 * 查询单个用户信息
	 * 
	 * @return
	 */
	private GraphQLFieldDefinition createUserField() {
		return GraphQLFieldDefinition.newFieldDefinition().name("user")
				.argument(newArgument().name("id").type(GraphQLInt).build()).type(userType).dataFetcher(environment -> {
					// 获取查询参数
					int id = environment.getArgument("id");

					// 执行查询, 这里随便用一些测试数据来说明问题
					User user = new User();
					user.setId(id);
					user.setAge(id + 15);
					user.setSex(id % 2);
					user.setName("Name_" + id);
					user.setPic("pic_" + id + ".jpg");
					return user;
				}).build();
	}

	/**
	 * 查询多个会员信息
	 * 
	 * @return
	 */
	private GraphQLFieldDefinition createUsersField() {
		return GraphQLFieldDefinition.newFieldDefinition().name("users")
				.argument(newArgument().name("page").type(GraphQLInt).build())
				.argument(newArgument().name("size").type(GraphQLInt).build())
				.argument(newArgument().name("name").type(GraphQLString).build()).type(new GraphQLList(userType))
				.dataFetcher(environment -> {
					// 获取查询参数
					int page = environment.getArgument("page");
					int size = environment.getArgument("size");
					String name = environment.getArgument("name");

					// 执行查询, 这里随便用一些测试数据来说明问题
					List<User> list = new ArrayList<>(size);
					for (int i = 0; i < size; i++) {
						User user = new User();
						user.setId(i);
						user.setAge(i + 15);
						user.setSex(i % 2);
						user.setName(name + "_" + page + "_" + i);
						user.setPic("pic_" + i + ".jpg");
						list.add(user);
					}
					return list;
				}).build();
	}

	public GraphQLSchema getSchema() {
		return schema;
	}

	public void setSchema(GraphQLSchema schema) {
		this.schema = schema;
	}
}
